// 源插件
import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import moment from 'moment';
import router from 'umi/router';

// 视图插件
import {
  Spin,
  Row,
  Col,
  Card,
  Form,
  Input,
  Select,
  Icon,
  Button,
  Dropdown,
  Menu,
  InputNumber,
  DatePicker,
  Modal,
  message,
  Badge,
  Divider,
  Steps,
  Radio,
  Upload,
  Empty,
  Table,
} from 'antd';
// 外部组件
import StandardTable from '@/components/StandardTable';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
// import { numDiv, numMulti } from '@/function.js';

// import styles from "../FundPosition/Test.less";
// import WrappedDynamicFieldSet from "./DynamicFieldSet";

// css OR less预编译器

// 图片

const FormItem = Form.Item;
function numDiv(num1, num2) {
  if (!num1) return null;
  let baseNum1 = 0;
  let baseNum2 = 0;
  let baseNum3;
  let baseNum4;
  try {
    baseNum1 = num1.toString().split('.')[1].length;
  } catch (e) {
    baseNum1 = 0;
  }
  try {
    baseNum2 = num2.toString().split('.')[1].length;
  } catch (e) {
    baseNum2 = 0;
  }
  baseNum3 = Number(num1.toString().replace('.', ''));
  baseNum4 = Number(num2.toString().replace('.', ''));
  return (baseNum3 / baseNum4) * Math.pow(10, baseNum2 - baseNum1);
}
function numMulti(num1, num2) {
  if (!num1) return null;
  let baseNum = 0;
  try {
    baseNum += num1.toString().split('.')[1].length;
  } catch (e) {}
  try {
    baseNum += num2.toString().split('.')[1].length;
  } catch (e) {}
  return (
    (Number(num1.toString().replace('.', '')) * Number(num2.toString().replace('.', ''))) /
    Math.pow(10, baseNum)
  );
}
@Form.create()
@connect(({ bonds, loading }) => ({
  bonds,
  loading: loading.models.bondsRule,
}))
class BillUpload extends PureComponent {
  componentWillMount() {}

  state = {
    formData: [],
  };

  onChange = e => {};

  okHandle() {
    const { record, isUpdate } = this.props;

    this.props.form.validateFields((err, values) => {
      // let inputUpdate = false;
      // if (isUpdate) {
      //   if (values.billId !== record.billId ||
      //     values.iscnumber !== record.billNumber ||
      //     values.frncoupon !== record.name ||
      //     values.redeemPrice !== record.redeemableType ||
      //     values.price !== record.amountOfMoney ||
      //     values.x !== record.discountRate ||
      //     values.p !== record.billCoupon ||
      //     values.transactionDate.format('YYYY-MM-DD') !== record.discoveryDate ||
      //     values.finalDate.format('YYYY-MM-DD') !== record.finalPriceDate
      //   ) {
      //     inputUpdate = true;
      //   }
      // }

      if (!err) {
        if (isUpdate) {
          const { dispatch } = this.props;
          dispatch({
            type: 'bonds/EditBonds',
            payload: {
              costPrice: values.costPrice ? numDiv(values.costPrice, 100) : null,
              coupon: values.coupon,
              grade: values.grade,
              isin: values.isin,
              id: record.id,
              lv: values.lv ? numDiv(values.lv, 100) : null,
              money: values.money,
              todayMarketValue: values.todayMarketValue,
              todayPrice: values.todayPrice,
              todayProfit: values.todayProfit,
              totalProfit: values.totalProfit,
              yesterdayPrice: values.yesterdayPrice,
              yieldLeverage: values.yieldLeverage ? numDiv(values.yieldLeverage, 100) : null,
              yieldMaturity: values.yieldMaturity ? numDiv(values.yieldMaturity, 100) : null,
              bank: values.bank,
              interestRate: values.interestRate ? numDiv(values.interestRate, 100) : null,
              todayPayInterest: values.todayPayInterest,
              ric: values.ric,
              duedate: values.duedate,
              nextPaymentDate: values.nextPaymentDate,
              couponFrequency: values.couponFrequency,
              updateFrequency: values.updateFrequency,
            },

            callback: res => {
              if (res.code === 1) {
                message.success('修改成功');
                this.props.closeRecord(true);
              } else {
                message.warning('修改失败');
              }
            },
          });
        } else {
          const { dispatch } = this.props;
          dispatch({
            type: 'bonds/AddBonds',
            payload: {
              // bondType: values.bondTypeToDisplay,
              bondTypeItem: values.bondTypeToDisplay,
              costPrice: values.costPrice ? numDiv(values.costPrice, 100) : null,
              coupon: values.coupon,
              grade: values.grade,
              // "id": 0,
              isin: values.isin,
              lv: values.lv ? numDiv(values.lv, 100) : null,
              name: values.name,
              priceOrigin: values.priceOrigin,
              todayMarketValue: values.todayMarketValue,
              todayPrice: values.todayPrice,
              todayProfit: values.todayProfit,
              totalProfit: values.totalProfit,
              yesterdayPrice: values.yesterdayPrice,
              yieldLeverage: values.yieldLeverage ? numDiv(values.yieldLeverage, 100) : null,
              yieldMaturity: values.yieldMaturity ? numDiv(values.yieldMaturity, 100) : null,
              bank: values.bank,
              interestRate: values.interestRate ? numDiv(values.interestRate, 100) : null,
              todayPayInterest: values.todayPayInterest,
              ric: values.ric,
              duedate: values.duedate,
              nextPaymentDate: values.nextPaymentDate,
              couponFrequency: values.couponFrequency,
              updateFrequency: values.updateFrequency,
            },

            callback: res => {
              if (res.code === 1) {
                message.success('添加成功');
                this.props.closeRecord(true);
              } else {
                message.warning('添加失败');
              }
            },
          });
        }
      }
    });
  }

  cancelHandle() {
    this.props.closeRecord(false);
  }

  render() {
    // const { } = this.state;
    const { record, form, RiskDetailVisible, title, isRy, isUpdate } = this.props;
    const { getFieldDecorator } = form;
    const config = {
      rules: [{ type: 'object', required: true, message: '请选择日期' }],
    };
    return (
      <Modal
        width={1280}
        destroyOnClose
        title={isUpdate ? '修改债券' : '添加债券'}
        visible={RiskDetailVisible}
        centered
        onOk={this.okHandle.bind(this)}
        onCancel={this.cancelHandle.bind(this)}
      >
        <Form layout="horizontal" onSubmit={this.handleSubmit} style={{ overflowY: 'scroll' }}>
          <div>
            <Row>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="类型" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('bondTypeToDisplay', {
                      initialValue: isUpdate
                        ? record.bondTypeToDisplay
                          ? record.bondTypeToDisplay
                          : null
                        : null,
                      rules: [
                        {
                          required: true,
                          message: '请填写类型',
                        },
                      ],
                    })(<Input style={{ width: '85%' }} placeholder="类型" disabled={isUpdate} />)}
                  </FormItem>
                </Col>
              </Col>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="所属银行" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('bank', {
                      initialValue: isUpdate ? record.bank : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写所属银行',
                        },
                      ],
                    })(
                      <Select placeholder="请选择银行">
                        <Option value="中银">中银</Option>
                        <Option value="瑞银">瑞银</Option>
                        <Option value="花旗">花旗</Option>
                      </Select>
                    )}
                  </FormItem>
                </Col>
              </Col>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="名称" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('name', {
                      initialValue: isUpdate ? record.name : null,
                      rules: [
                        {
                          required: true,
                          message: '请填写名称',
                        },
                      ],
                    })(<Input style={{ width: '85%' }} placeholder="名称" disabled={isUpdate} />)}
                  </FormItem>
                </Col>
              </Col>
            </Row>
            <Row>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="到期日" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('duedate', {
                      initialValue: isUpdate ? record.duedate : null,
                      rules: [
                        {
                          required: false,
                          message: '请输入到期日',
                        },
                      ],
                    })(<Input style={{ width: '85%' }} placeholder="请输入到期日" />)}
                  </FormItem>
                  {/* <FormItem label="结束日期" labelCol={{ span: 8 }} wrapperCol={{ span: 15 }}>
                    {form.getFieldDecorator('edate', {
                      initialValue: moment(row.edate),
                      rules: [{ type: 'object' }],
                    })(
                      <DatePicker
                        style={{ width: '85%' }}
                        showTime
                        format="YYYY-MM-DD"
                        placeholder="结束日期"
                      />
                    )}
                  </FormItem> */}
                </Col>
              </Col>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="下一付息日" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('nextPaymentDate', {
                      initialValue: isUpdate ? record.nextPaymentDate : null,
                      rules: [
                        {
                          required: false,
                          message: '请输入下一付息日',
                        },
                      ],
                    })(<Input style={{ width: '85%' }} placeholder="请输入下一付息日" />)}
                  </FormItem>
                </Col>
              </Col>

              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="金额/单位" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('money', {
                      initialValue: isUpdate ? record.money : null,
                      rules: [
                        {
                          required: true,
                          message: '请填写金额/单位',
                        },
                      ],
                    })(<InputNumber style={{ width: '85%' }} placeholder="金额/单位" />)}
                  </FormItem>
                </Col>
              </Col>
            </Row>
            <Row>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="成本价" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('costPrice', {
                      initialValue: isUpdate ? numMulti(record.costPrice, 100) : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写成本价',
                        },
                      ],
                    })(
                      <InputNumber
                        style={{ width: '85%' }}
                        placeholder="成本价"
                        formatter={value => `${value}%`}
                      />
                    )}
                  </FormItem>
                </Col>
              </Col>

              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="昨日价" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('yesterdayPrice', {
                      initialValue: isUpdate ? record.yesterdayPrice : null,
                      rules: [
                        {
                          required: false,
                          // message: '请填写昨日价',
                        },
                      ],
                    })(<InputNumber style={{ width: '85%' }} placeholder="昨日价" />)}
                  </FormItem>
                </Col>
              </Col>

              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="今日价" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('todayPrice', {
                      initialValue: isUpdate ? record.todayPrice : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写今日价',
                        },
                      ],
                    })(<InputNumber style={{ width: '85%' }} placeholder="今日价" />)}
                  </FormItem>
                </Col>
              </Col>
            </Row>
            <Row>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="今日市值" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('todayMarketValue', {
                      initialValue: isUpdate ? record.todayMarketValue : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写今日市值',
                        },
                      ],
                    })(<InputNumber style={{ width: '85%' }} placeholder="今日市值" />)}
                  </FormItem>
                </Col>
              </Col>

              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="今日盈亏" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('todayProfit', {
                      initialValue: isUpdate ? record.todayProfit : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写今日盈亏',
                        },
                      ],
                    })(<InputNumber style={{ width: '85%' }} placeholder="今日盈亏" />)}
                  </FormItem>
                </Col>
              </Col>

              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="总盈亏" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('totalProfit', {
                      initialValue: isUpdate ? record.totalProfit : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写总盈亏',
                        },
                      ],
                    })(<InputNumber style={{ width: '85%' }} placeholder="总盈亏" />)}
                  </FormItem>
                </Col>
              </Col>
            </Row>
            <Row>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="到期收益率" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('yieldMaturity', {
                      initialValue: isUpdate ? numMulti(record.yieldMaturity, 100) : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写到期收益率',
                        },
                      ],
                    })(
                      <InputNumber
                        style={{ width: '85%' }}
                        placeholder="到期收益率"
                        formatter={value => `${value}%`}
                      />
                    )}
                  </FormItem>
                </Col>
              </Col>

              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="杠杆后收益率" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('yieldLeverage', {
                      initialValue: isUpdate ? numMulti(record.yieldLeverage, 100) : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写杠杆后收益率',
                        },
                      ],
                    })(
                      <InputNumber
                        style={{ width: '85%' }}
                        placeholder="杠杆后收益率"
                        formatter={value => `${value}%`}
                        // parser={value => value.replace('%', '') * 1 / 100}
                      />
                    )}
                  </FormItem>
                </Col>
              </Col>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="ISIN" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('isin', {
                      initialValue: isUpdate ? record.isin : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写ISIN',
                        },
                      ],
                    })(<Input style={{ width: '85%' }} placeholder="ISIN" />)}
                  </FormItem>
                </Col>
              </Col>
            </Row>
            <Row>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="票息" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('coupon', {
                      initialValue: isUpdate ? record.coupon : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写票息',
                        },
                      ],
                    })(<Input style={{ width: '85%' }} placeholder="票息" />)}
                  </FormItem>
                </Col>
              </Col>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="lv" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('lv', {
                      initialValue: isUpdate ? numMulti(record.lv, 100) : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写lv',
                        },
                      ],
                    })(
                      <InputNumber
                        style={{ width: '85%' }}
                        placeholder="lv"
                        formatter={value => `${value}%`}
                      />
                    )}
                  </FormItem>
                </Col>
              </Col>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="评级" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('grade', {
                      initialValue: isUpdate ? record.grade : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写评级',
                        },
                      ],
                    })(<Input style={{ width: '85%' }} placeholder="评级" />)}
                  </FormItem>
                </Col>
              </Col>
            </Row>
            <Row>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="利率" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('interestRate', {
                      initialValue: isUpdate ? numMulti(record.interestRate, 100) : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写利率',
                        },
                      ],
                    })(
                      <InputNumber
                        style={{ width: '85%' }}
                        placeholder="利率"
                        formatter={value => `${value}%`}
                      />
                    )}
                  </FormItem>
                </Col>
              </Col>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="今日付息" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('todayPayInterest', {
                      initialValue: isUpdate ? record.todayPayInterest : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写今日付息',
                        },
                      ],
                    })(<InputNumber style={{ width: '85%' }} placeholder="今日付息" />)}
                  </FormItem>
                </Col>
              </Col>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="派息频率" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('couponFrequency', {
                      initialValue: (record.couponFrequency || '').toString(),
                    })(
                      <Select style={{ width: '85%' }}>
                        <Option value="不派息">不派息</Option>
                        <Option value="每月">每月</Option>
                        <Option value="每季度">每季度</Option>
                        <Option value="每半年">每半年</Option>
                      </Select>
                    )}
                  </FormItem>
                </Col>
              </Col>
            </Row>
            <Row>
              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="更新周期" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('updateFrequency', {
                      initialValue: (record.updateFrequency || '').toString(),
                    })(
                      <Select style={{ width: '85%' }}>
                        <Option value="日更">日更</Option>
                        <Option value="月更">月更</Option>
                      </Select>
                    )}
                  </FormItem>
                </Col>
              </Col>

              <Col xl={7} lg={24} md={24} sm={24} xs={24} offset={1}>
                <Col span={24}>
                  <FormItem label="RIC" labelCol={{ span: 6 }} wrapperCol={{ span: 15 }}>
                    {getFieldDecorator('ric', {
                      initialValue: isUpdate ? record.ric : null,
                      rules: [
                        {
                          required: false,
                          message: '请填写RIC',
                        },
                      ],
                    })(<Input style={{ width: '85%' }} placeholder="RIC" />)}
                  </FormItem>
                </Col>
              </Col>
            </Row>
          </div>
          {/* <WrappedDynamicFieldSet 
            onRef={this.onRef}
            closeRecord={this.closeRecord.bind(this)}
            record={isUpdate ? record.productDataDetailForms : []}
            isUpdate={isUpdate}
            handleSubmit1={this.okHandle.bind(this)}
          /> */}
        </Form>
      </Modal>
    );
  }

  onRef = ref => {
    this.child = ref;
  };

  componentDidMount() {}

  componentWillUnmount() {}
}

export default BillUpload;
